/// RefCell\RC\Box
/// RefCell<T>\RC<T> 与 Mutex<T>\Arc<T> 区别
/// 1. Mutex<T> 提供内部可变性，类似于 RefCell
/// 2. RefCell<T>\RC<T>是非线程安全
/// 3. Mutex<T>\Arc<T> 是线程安全
use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    // 定义一个计数器
    // Rc<T> 不是线程安全的
    let counter = Arc::new(Mutex::new(0));

    // let counter = Rc::new(Mutex::new(0));
    let mut handles = vec![];
    for _ in 0..10 {
        // let cnt = Rc::new(&counter);
        let counter = Arc::clone(&counter);
        let handle = thread::spawn(move || {
           let mut num = counter.lock().unwrap();
            *num += 1;
        });
        handles.push(handle);
    };
    for handle in handles {
        handle.join().unwrap();
    }
    println!("Result: {}", *counter.lock().unwrap());
    println!("Hello, world!");
}
